#!/usr/bin/env bash

# ----------------------------------------------------------------------
# Filename:   05-conf-nrjb.sh
# Version:    1.0
# Date:       2020/07/30
# Author:     Lz
# Email:      lz843723683@gmail.com
# History：     
#             Version 1.0, 2020/07/30
# Function:   测试NRJB 配置文件内容
# Out:        
#              0 => TPASS
#              1 => TFAIL
#              other=> TCONF
# ----------------------------------------------------------------------

## TODO : 测试收尾清除工作
#
ConfClean_FTNRJB(){
	return $TPASS
}


## TODO: 调用程序退出函数
#    In: $1 => 调用脚本结果值
ConfExit_FTNRJB(){
        # 调用退出函数，其中调用了clean相关函数
	Exit_FTLIB ${1}
}


## TODO: 用户界面
#
ConfUSAGE_FTNRJB(){
	USAGE_FTLIB "NRJB - 配置文件内容"
}


## TODO : 测试前的初始化 
#  Out  : 
#         0 => TPASS
#         1 => TFAIL
#         other=> TCONF
ConfInit_FTNRJB(){
        # 调用初始化函数
	Init_FTLIB "ConfClean_FTNRJB"
        # 调用用户界面函数
 	ConfUSAGE_FTNRJB
}


## TODO : 测试NRJB  /etc/sysctl.conf文件中添加指定内容
#
ConfTest01_FTNRJB(){
	local filepath="/etc/sysctl.conf"
        local flagarr=([0]="kernel.sem=512 32000 100 256" \
		[1]="net.core.rmem_default=262144" \
		[2]="net.core.wmem_default=262144" \
		[3]="kernel.core_uses_pid =1" \
		[4]="kernel.shmmax = 4294967295" \
		[5]="kernel.shmmni = 4096" \
		[6]="kernel.shmall = 2097152" \
		[7]="fs.file-max = 6815744" \
		[8]="net.ipv4.ip_local_port_range = 9000 65500" \
		[9]="net.core.rmem_max = 4194304" \
		[10]="net.core.wmem_max = 1048576" \
		[11]="fs.aio-max-nr = 1048576")	
	# 常规打印
	echo "配置文件${filepath}中要求存在以下配置:"

	local index=""
	local tmpnum=${#flagarr[@]}
	local failstr=""
	let tmpnum=tmpnum-1
	for index in `seq 0 $tmpnum`
	do
		echo "${flagarr[$index]}"

		cat $filepath | grep -q "^${flagarr[$index]}"
		if [ $? -ne 0 ];then
			failstr="${failstr} ${flagarr[$index]}\n"
		fi
	done

	if [ "Z${failstr}" != "Z" ];then
		echo ""
		echo "Error: 文件${filepath}中不存在以下配置:"
		echo -e "${failstr}"
		OutputRet_FTLIB ${TFAIL}
		RetParse_FTLIB "要求文件${filepath}中存在指定配置" "False"
	else
		OutputRet_FTLIB ${TPASS}
		RetParse_FTLIB "要求文件${filepath}中存在指定配置" "False"	
	fi
}


## TODO : 测试NRJB /etc/security/limits.conf文件中添加指定内容
#
ConfTest02_FTNRJB(){
	local filepath="/etc/security/limits.conf"
        local flagarr=([0]="root    soft    nofile   65536" \
		[1]="root    hard    nofile   65536" \
		[2]="ems    soft    nproc   65536" \
		[3]="ems    hard    nproc   65536" \
		[4]="mysql    soft    nproc   65536" \
		[5]="mysql    hard    nproc   65536")

	# 常规打印
	echo "配置文件${filepath}中要求存在以下配置:"

	local index=""
	local tmpnum=${#flagarr[@]}
	local failstr=""
	let tmpnum=tmpnum-1
	for index in `seq 0 $tmpnum`
	do
		echo "${flagarr[$index]}"

		cat $filepath | grep -q "^${flagarr[$index]}"
		if [ $? -ne 0 ];then
			failstr="${failstr} ${flagarr[$index]}\n"
		fi
	done

	if [ "Z${failstr}" != "Z" ];then
		echo ""
		echo "Error: 文件${filepath}中不存在以下配置:"
		echo -e "${failstr}"
		OutputRet_FTLIB ${TFAIL}
		RetParse_FTLIB "要求文件${filepath}中存在指定配置" "False"
	else
		OutputRet_FTLIB ${TPASS}
		RetParse_FTLIB "要求文件${filepath}中存在指定配置" "False"	
	fi
}


## TODO : Main
#  Out  : 
#         0 => TPASS
#         1 => TFAIL
#         other => TCONF
ConfMain_FTNRJB(){
	ConfInit_FTNRJB

	ConfTest01_FTNRJB

	ConfTest02_FTNRJB

	return ${TPASS}	
}

ConfMain_FTNRJB
ConfExit_FTNRJB $?
